//封装表单验证
class Vaild {
  constructor(dom, vaildFiled, successFn, resetFn) {
    this.dom = dom;
    this.vaildFiled = vaildFiled;
    this.successFn = successFn;
    this.resetFn = resetFn;
    this.init();
    this.bindSuccessEvent();
    this.bindReset();
  }
  init() {
    console.log(this.vaildFiled);
    this.dom.bootstrapValidator({
      //1. 指定不校验的类型，默认为[':disabled', ':hidden', ':not(:visible)'],可以不设置
      excluded: [],

      //2. 指定校验时的图标显示，默认是bootstrap风格
      feedbackIcons: {
        valid: "glyphicon glyphicon-ok",
        invalid: "glyphicon glyphicon-remove",
        validating: "glyphicon glyphicon-refresh"
      },
      //3. 指定校验字段
      fields: this.vaildFiled
    });
  }
  getValidator() {
    return this.dom.data("bootstrapValidator");
  }
  bindSuccessEvent() {
    this.dom.on("success.form.bv", (e) => {
      e.preventDefault();

      //使用ajax提交逻辑
      this.successFn.call(this);
    });
  }
  bindReset() {
    const validator = this.getValidator();
    this.dom.on("reset", () => {
      validator.resetForm(true); //重置表单，并且会隐藏所有的错误提示和图标
      this.resetFn && this.resetFn.call(this);
    });
  }
}
